<?php 
namespace Admin\Controller;

class PhotoController extends AdminController{

//查询用户相册信息

	public function index(){
		$data = new \Think\Model();
		$result = $data->query("select p.id,i.uid iid,i.sex,i.birthday_year,i.birthday_month,i.birthday_day,i.email,u.id nid,u.name uname,p.id pid,p.uid,p.name pname,p.is_face from love_user u,love_userphoto p,love_userinfo i where u.id = p.uid and p.uid=i.uid and p.is_face = 1 order by p.id desc");
		/*$result2 = $data->query("select p.uid,i.uid,i.sex from love_userphoto p,love_userinfo i where p.uid = i.uid");*/
		/*$result1 = array_merge($result,$result2);*/
		var_dump($result);
		/*echo $data->getLastSql();*/
		$this->assign('list',$result);
		$this->display();
	}

	public function edit(){
		$id = I('get.pid/d',0);
		$data = M('userphoto')->find($id);

		if ($data === null) {
			$this->redirect('index');
			return;
		}
		$this->assign('item',$data);
		$this->display();
	}

	public function upload(){

		$upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();  

        $fileName='C:/wamp/www/love/object/app/Uploads'.'/'.$info['avatar_file']['savepath'];

        $imgName=$info['avatar_file']['savename'];

        import("Org.Util.picture");
        $imgDeal=new \picture( $fileName.$imgName);
         
        $code=$_POST['avatar_data'];

        $code_deal=json_decode($code);
          
        $temp=$code_deal->x;
        $imgDeal->CUT_X=$code_deal->x;
        $imgDeal->CUT_Y=$code_deal->y;
        $imgDeal->CUT_WIDTH=$code_deal->width;
        $imgDeal->CUT_HEIGHT=$code_deal->height;
       
       	 $imgDeal->CUT();

         imagejpeg($imgDeal->TRUE_COLOR,$fileName.$temp.$imgName);   ///输出图片 此处待修改
         /*var_dump($fileName.$temp.$imgName);*/
       if(!$info) {// 上传错误提示错误信息
             $this->error($upload->getError());
             }else{// 上传成功
        	/*$this->success('index');*/
        	//将图片信息存入数据库
       			
		      /* var_dump(__ROOT__.'/Uploads/'.$info['avatar_file']['savepath'].$temp.$imgName);*/
		       $img['name']=__ROOT__.'/Uploads/'.$info['avatar_file']['savepath'].$temp.$imgName;
		       $img['uid']=I('get.id/d');
		       $img['is_face']=1;
		       /*var_dump($img);*/
		       $uid=$img['uid'];
		       var_dump($uid);
		       M('userphoto')->is_face=0;
		       M('userphoto')->where("uid=$uid")->save();
		       M('userphoto')->add($img);
		        }

       			

	}

	public function delete(){
		$uid=I('get.id/d');
		/*var_dump($uid);*/
		$data = new \Think\Model();
		$result=$data ->query("select id from love_userphoto where uid = $uid and is_face = 1");
		/*var_dump($result);*/
		$id = $result[0]['id'];
		/*var_dump($id);*/
		$name=__ROOT__.'/Public/Images/touxiang.jpg';
		
		if(M('userphoto')->delete("$id")){
			
			M('userphoto')->is_face = 1;
			M('userphoto')->where("name='$name' and uid=$uid")->save();
				/*echo $data->getLastSql();*/
				$this->success('删除成功', U('index'));
			}
			
			
		else{

			$this->error('删除失败');
		}

	}

	public function select(){
		$uid=I('get.iid/d');
		/*var_dump($uid);*/
		$data = M('userphoto')->where("uid=$uid")->select();
		var_dump($data);
		$this->assign('list',$data);
		$this->display();
	}

	public function imgdelete(){
		$id=I('get.id/d',0);
		$uid=I('get.uid/d',0);
		$is_face = I('get.is_face/d',0);
		$name=__ROOT__.'/Public/Images/touxiang.jpg';
		/*var_dump($id);*/
		if(M('userphoto')->delete("$id")>0){
			if($is_face == 1){
			M('userphoto')->is_face = 1;
			M('userphoto')->where("name='$name' and uid=$uid")->save();
				$this->success('删除成功', U('select',array('iid'=>"$uid")));
			}else{
				$this->success('删除成功', U('select',array('iid'=>"$uid")));
			}			
		}else{
			$this->error('删除失败');
		}
	}
}
